import { fetchList } from '@/api/product'
import { createOutboundOrder } from '@/api/outbound-order'
import Pagination from '@/components/Pagination'

export default {
  name: 'Outbound-Order',
  components: { Pagination },

  data(){
    return{
      tableList:[],
      nullItem: {
        name:null,
        item_id:null,
        price:null,
        quantity:null,
        total:null
      },
      itemVisible:false,
      insertIndex: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        category: 0,
        sub_category: 0,
        category_id: '',
        product_name: ''
      },
      cateList:[],
      categoryOptions: [{
        value: 0,
        label: '全部'
      }],
      subCategoryOptions: [{
        value: 0,
        label: '全部'
      }],
      checkedList: [],
      selectedList: [],
      statusOptions: [
        {
          value: 'allocate',
          label: '调拨'
        }, {
          value: 'sale',
          label: '销售'
        }, {
          value: 'loss',
          label: '盘亏'
        }, {
          value: 'return',
          label: '采购退货'
        }, {
          value: 'other',
          label: '其他出库'
        }
      ],
      type: null,
      remark: null
    }
  },

  created(){
    this.init()
  },

  methods:{

    init(){
      this.tableList = []
      for(let i = 0; i < 8; i++){
        let temp = this.nullItem
        temp.id = i
        this.tableList.push(temp)
      }
    },

    showSelectDialog: async function(insertIndex){
      this.insertIndex = insertIndex
      if(this.cateList.length == 0){
        const cateList = await this.$store.dispatch('product/getCateList')
        this.categoryOptions = this.categoryOptions.concat(cateList)
        this.cateList = cateList
      }
      this.list = []
      this.total = 0
      this.getList()
      this.itemVisible = true
      this.checkedList = []
      this.selectedList = []
    },

    getList() {
      fetchList(this.listQuery).then(response => {
        this.list = response.products
        this.total = response.total
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        Promise.reject(error)
      })
    },

    handleSearch() {
      this.list = []
      this.listQuery.page = 1
      this.getList()
    },

    handleSelectChange() {
      const id = this.listQuery.category
      const list = [{
        value: 0,
        label: '全部'
      }]
      if (id === 0) {
        this.subCategoryOptions = list
      } else {
        const index = this.categoryOptions.findIndex(v => v.value === id)
        const item = this.categoryOptions[index]
        this.subCategoryOptions = list.concat(item.children)
      }
      this.listQuery.sub_category = 0
      this.handleSearch()
    },

    removeLine(index){
      if(this.tableList.length > 1){
        this.tableList.splice(index,1)
      }
    },

    addLine(index){
      this.tableList.splice(index + 1,0,this.nullItem)
    },

    select(row,item){
      let checkedList = this.checkedList
      let list = this.selectedList
      if(checkedList.indexOf(item.code) > -1){
        checkedList.splice(checkedList.indexOf(item.code),1)
        for(let i in list){
          if(item.code == list[i].code){
            list.splice(i,1)
            break
          }
        }
      }else{
        checkedList.push(item.code)
        item.product_name = row.name + ' ' + item.name
        item.category = row.category.name
        item.parent_category = row.category.parent.name
        list.push(item)
      }
      this.checkedList = checkedList
      this.selectedList = list
    },

    updateTotal(index){
      if(parseFloat(this.tableList[index].price) && parseInt(this.tableList[index].quantity)){
        let number = new Number(parseFloat(this.tableList[index].price) * parseInt(this.tableList[index].quantity))
        this.tableList[index].total = number.toFixed(2)
      }else{
        this.tableList[index].total = null
      }
    },

    selectConfirm(){
      let insertIndex = this.insertIndex
      let tableList = this.tableList
      let selectedList = this.selectedList
      for(let i in selectedList) {
        let item = {
          item_id: selectedList[i].id,
          name: selectedList[i].product_name,
          price: selectedList[i].member_price,
          category: selectedList[i].category,
          parent_category: selectedList[i].parent_category,
          quantity: null
        }
        if(0 == i){
          tableList.splice(insertIndex,1,item)
        }else{
          tableList.splice(insertIndex + 1,0,item)
        }
      }

      this.itemVisible = false
      this.tableList = tableList
    },

    handleCreate(){
      let list = this.tableList
      let remark = this.remark
      let type = this.type

      if(!type){
        this.$message.error('请选择业务类型');
        return
      }

      let data = []
      let flag = false
      for(let i in list){
        if(list[i].item_id){
          flag = true
          if(!list[i].price || !list[i].quantity){
            this.$message.error('请填写正确的单价和数量');
            return
          }else{
            data.push({
              id:list[i].item_id,
              quantity:list[i].quantity,
              price:list[i].price
            })
          }
        }
      }

      if(!flag){
        this.$message.error('请选择出库物资');
        return
      }

      createOutboundOrder({
        product_items:data,
        type: type,
        remark: remark
      }).then(res => {
        this.init()
        this.$notify({
          title: '成功',
          message: '创建成功',
          type: 'success',
          duration: 2000
        })
      })
    }

  }
}
